# -- coding: utf-8 --
import threading
import queue

# 实现生产者消费者模型
# q = queue.Queue()
#
# def produce():
#     for item in ['a','b','c','d','e','f','g','h']:
#         q.put(item)
#         print('produce %s' % item)
#
# def consume():
#     for i in range(8):
#         item = q.get()
#         print('consume %s' % item)
#
# producer = threading.Thread(target=produce(),args=())
# consumer = threading.Thread(target=consume(),args=())
# producer.start()
# consumer.start()
# producer.join()
# consumer.join()

# 实现生产者、计算者、消费者模型

# q1 = queue.Queue()
# q2 = queue.Queue()
#
# def produce():
#     for item in ['a','b','c','d','e','f','g','h']:
#         q1.put(item)
#         print('produce %s' % item)
#
# def compute():
#     for i in range(8):
#         item = q1.get()
#         item = item.upper()
#         q2.put(item)
#         print('compute %s' % item)
#
# def consume():
#     for i in range(8):
#         item = q2.get()
#         print('consumer %s' % item)
#
# producer = threading.Thread(target=produce(),args=())
# computer = threading.Thread(target=compute(),args=())
# consumer = threading.Thread(target=consume(),args=())
# producer.start()
# computer.start()
# consumer.start()
# producer.join()
# computer.join()
# consumer.join()


# 同步生产者与消费者的推进速度

q = queue.Queue()

def produce():
    for item in ['A','B','C','D']:
        q.put(item)
        print('produce %s' % item)
    q.join()
    print('---------- q is empty')

    for item in ['E','F','G','H']:
        q.put(item)
        print('produce %s' % item)
    q.join()
    print('---------- q is empty')

def consume():
    for i in range(8):
        item = q.get()
        print('consume %s' % item)
        q.task_done()


producer = threading.Thread(target=produce, args=())
consumer = threading.Thread(target=consume, args=())
producer.start()
consumer.start()


